﻿using PLCS.App.Events;
using PLCS.App.Shared.Models;
using PLCS.App.Shared.Service;
using PLCS.App.Shared.Service.Dialog;
using PLCS.App.Shared.ViewModels;
using PLCS.Application.Contracts.LogShowApp.Dtos;
using PLCS.Client.IService.LogManager;
using PLCS.Shared.Common.Constant;

using Prism.Services.Dialogs;

using System.Windows.Controls;

namespace PLCS.App.ViewModels;

public class MainWindowViewModel : BaseViewModel
{
    private readonly IRegionManager _regionManager;
    private readonly IContainerProvider _containerProvider;
    private readonly IEventAggregator _eventAggregator;
    private readonly IDialogService _dialogService;
    private readonly IOperationLogService _logShowService;
    private readonly CacheSettingService _cacheSettingService;
    private readonly IHostDialogService _hostDialogService;

    private string _title = "PLCS";

    public string Title
    {
        get { return _title; }
        set { SetProperty(ref _title, value); }
    }

    private ObservableCollection<MenuDto> _itemList;

    public ObservableCollection<MenuDto> ItemList
    {
        get { return _itemList; }
        set { SetProperty(ref _itemList, value); }
    }

    public CacheSetting CacheSetting { get; set; }

    private bool _isDrawerOpen;

    public bool IsDrawerOpen
    {
        get { return _isDrawerOpen; }
        set { SetProperty(ref _isDrawerOpen, value); }
    }

    private int _isShowLogo;

    public int IsShowLogo
    {
        get { return _isShowLogo; }
        set { SetProperty(ref _isShowLogo, value); }
    }

    public DelegateCommand InitCommand { get; }
    public DelegateCommand<MenuDto> NavagiteCommand { get; }
    public DelegateCommand GoHomeCommand { get; }
    public DelegateCommand<Window> MinCommand { get; }
    public DelegateCommand<Window> MaxCommand { get; }
    public DelegateCommand<Window> CloseCommand { get; }
    public DelegateCommand ChangePassWordCommand { get; }
    public DelegateCommand LoginOutCommand { get; }
    public DelegateCommand SettingCommand { get; }
    public DelegateCommand ModelingCommand { get; }

    public MainWindowViewModel(IRegionManager regionManager, IContainerProvider containerProvider, IEventAggregator eventAggregator, IDialogService dialogService, IOperationLogService logShowService, CacheSettingService cacheSettingService)
    {
        _regionManager = regionManager;
        _containerProvider = containerProvider;
        _eventAggregator = eventAggregator;
        _dialogService = dialogService;
        _logShowService = logShowService;
        _cacheSettingService = cacheSettingService;
        _eventAggregator.GetEvent<MenuEvent>().Subscribe(MenuSub);
        _hostDialogService = containerProvider.Resolve<IHostDialogService>();

        InitCommand = new DelegateCommand(Init);
        NavagiteCommand = new DelegateCommand<MenuDto>(Navagite);
        MinCommand = new DelegateCommand<Window>(Min);
        MaxCommand = new DelegateCommand<Window>(Max);
        CloseCommand = new DelegateCommand<Window>(Close);
        ChangePassWordCommand = new DelegateCommand(ChangePassWord);
        LoginOutCommand = new DelegateCommand(LoginOut);
        GoHomeCommand = new DelegateCommand(GoHome);
        SettingCommand = new DelegateCommand(Setting);
        ModelingCommand = new DelegateCommand(Modeling);
    }

    private void Modeling()
    {
        _regionManager.RequestNavigate(Shared.ConstantManagers.RegionManager.MainContextRegion, "ModelingView");
    }

    private async void Setting()
    {
        await _hostDialogService.ShowDialogAsync("SettingView");
    }

    private void GoHome()
    {
        _regionManager.RequestNavigate(Shared.ConstantManagers.RegionManager.MainContextRegion, "MonitorView");
    }

    private async void ChangePassWord()
    {
        await _hostDialogService.ShowDialogAsync("ChangePasswordView");
    }

    #region 退出登录

    private void LoginOut()
    {
        App.LoginOut(_containerProvider);
    }

    #endregion 退出登录

    #region 导航/最大化/最小化/关闭

    private void Navagite(MenuDto menu)
    {
        if (menu != null && !string.IsNullOrEmpty(menu.ViewName))
        {
            _regionManager.RequestNavigate(Shared.ConstantManagers.RegionManager.MainContextRegion, menu.ViewName);

            IsDrawerOpen = false;
        }
    }

    private void Min(Window w)
    {
        w.WindowState = WindowState.Minimized;
    }

    private void Max(Window w)
    {
        if (w.WindowState == WindowState.Maximized)
            w.WindowState = WindowState.Normal;
        else
            w.WindowState = WindowState.Maximized;
    }

    private void Close(Window w)
    {
        if (ShowQuestion("确认关闭吗"))
        {
            w.Close();
            Environment.Exit(0);
        }
    }

    private async void Init()
    {
        CacheSetting = await _cacheSettingService.InitalCacheSetting();
        GlobalVariable.PageSize = CacheSetting.PageSize;
        IsShowLogo = CacheSetting.IsShowLogo;
        _regionManager.RequestNavigate(Shared.ConstantManagers.RegionManager.MainContextRegion, "MonitorView");
    }

    #endregion 导航/最大化/最小化/关闭

    public void MenuSub(ObservableCollection<MenuDto> menus)
    {
        ItemList = menus;
    }
}